package org.beetl.sql.ext.jpa.core;

import java.util.Map;

import javax.persistence.Table;

import org.beetl.sql.clazz.NameConversion;
import org.beetl.sql.core.DefaultNameConversion;

public class JPA3BeetlsqlNameConversion extends NameConversion{
	
	NameConversion nc = null;
	public JPA3BeetlsqlNameConversion(){
		nc=new DefaultNameConversion();
	}
	/**
	 * 对于没有jpa注解的，采用下划线命名方式
	 * @param nc
	 */
	public JPA3BeetlsqlNameConversion(NameConversion nc){
		this.nc = nc !=null ? nc:new DefaultNameConversion();
	}
	
	@Override
	public String getColName(Class<?> c, String attrName) {
		if(c==null||Map.class.isAssignableFrom(c)){
			return nc!=null?nc.getColName(attrName):attrName;
		}
		return JPA3EntityHelper.getEntityTable(c,nc).getCol(attrName);
	}

	@Override
	public String getPropertyName(Class<?> c, String colName) {
		if(c==null||Map.class.isAssignableFrom(c)){
			return nc!=null?nc.getPropertyName(c, colName):colName;
		}
		//col到property是可能有对应关系的，即使property被标注了Transient
		 String prop = JPA3EntityHelper.getEntityTable(c,nc).getProp(colName);
		 if(prop!=null) {
			 return prop;
		 }
		 return nc!=null?nc.getPropertyName(c, colName):colName;
	}

	@Override
	public String getTableName(Class<?> c) {
		Table table = (Table)c.getAnnotation(Table.class);
		if(table!=null){
			return table.name();
		}
		return JPA3EntityHelper.getEntityTable(c,nc).getName();
	}
	
}
